package com.godpaper.the3directs.busniess.managers
{
	import com.godpaper.the3directs.model.BoardModel;
	import com.godpaper.the3directs.model.PiecesModel;
	import com.godpaper.the3directs.model.PlayerModel;
	import com.godpaper.the3directs.util.PositionTransactUtil;
	import com.godpaper.the3directs.views.components.GasketButton;
	import com.godpaper.the3directs.views.components.PieceButton;
	import com.godpaper.the3directs.vo.PieceVO;
	
	import de.polygonal.ds.Array2;
	
	import flash.geom.Point;
	
	import jp.dip.hael.gameai.graph.Node;
	import jp.dip.hael.gameai.graph.WeightedEdge;
	
	import mx.controls.Alert;
	
	public class PiecesManager
	{
		public function PiecesManager()
		{
		}
		
		public static function tryMovement(target:PieceButton,destination:Point,position:Array):Boolean
		{
			//			trace("target name: ",target.pieceVO.name,"destination original point: ",destination.x,destination.y);
			//refresh gamePosition
			trace("target name@tryMovement:",target.pieceVO.name);
			trace("target original position@tryMovement:",target.pieceVO.position[0],target.pieceVO.position[1]);
			trace("gamePosition before movement@tryMovement:",BoardModel.getInstance().gamePosition.dump());
			
			//cover protected position
			
			//target movement;
			target.move( destination.x,destination.y);
			//			trace("target new point: ",Application.application.localToGlobal(new Point(target.x,target.y)).toString());
			trace("target new position@tryMovement:",position.toString());
			
			//refresh new position;
			if(PiecesModel.getInstance().selectedPiece.pieceVO.name.indexOf(PieceButton.NAME_PREFIX_RED)!=-1)
			{
				(PiecesModel.getInstance().redPiecesCollection.getItemAt(PiecesModel.getInstance().redPiecesCollection.getItemIndex(target)) as PieceButton).pieceVO.position = position;
			}else
			{
				(PiecesModel.getInstance().bluePiecesCollection.getItemAt(PiecesModel.getInstance().bluePiecesCollection.getItemIndex(target)) as PieceButton).pieceVO.position = position;
			}
			
			//refresh gamePosition
			BoardModel.getInstance().gamePosition.sett(position[0],position[1],target);
			trace("gamePosition after arrived@tryMovement:",BoardModel.getInstance().gamePosition.dump());
			
			var destPositionIndex:int = PositionTransactUtil.transactArray2Index(position);
			trace("destPositionIndex:",destPositionIndex);
			//set color to gasketButton and disable it,and refresh gameGrid
			if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
			{
				if(!PositionTransactUtil.whetherRotate180Degree(destPositionIndex))
				{
					(BoardModel.getInstance().gasketsPosition.gett(position[0],position[1]) as GasketButton).styleName=GasketButton.STYLE_TRIANGLE_BLUE;
				}else
				{
					(BoardModel.getInstance().gasketsPosition.gett(position[0],position[1]) as GasketButton).styleName=GasketButton.STYLE_TRIANGLE_BLUE_R180;
				}
				//gameGridBlue
				BoardModel.getInstance().gameGridBlue.setWalkable(position[0],position[1],true);
				//add to blueCollection;
				PiecesModel.getInstance().blueGasketsCollection.addItem( BoardModel.getInstance().gasketsPosition.gett(position[0],position[1]) as GasketButton );
			}else
			{
				if(!PositionTransactUtil.whetherRotate180Degree(destPositionIndex))
				{
					(BoardModel.getInstance().gasketsPosition.gett(position[0],position[1]) as GasketButton).styleName=GasketButton.STYLE_TRIANGLE_RED;
				}else
				{
					(BoardModel.getInstance().gasketsPosition.gett(position[0],position[1]) as GasketButton).styleName=GasketButton.STYLE_TRIANGLE_RED_R180;
				}
				//gameGridRed
				BoardModel.getInstance().gameGridRed.setWalkable(position[0],position[1],true);
				//add to redsCollection;
				PiecesModel.getInstance().redGasketsCollection.addItem( BoardModel.getInstance().gasketsPosition.gett(position[0],position[1]) as GasketButton );
			}
			var gbDestination:GasketButton = (BoardModel.getInstance().gasketsPosition.gett(position[0],position[1]) as GasketButton);
			trace("gbDestination:",gbDestination,"\n\t","~left:",gbDestination.left,"\n\t","~top:",gbDestination.top,"\n\t","~right:",gbDestination.right,"\n\t","~bottom:",gbDestination.bottom,"\n\t");
			gbDestination.enabled = false;
			//remove selected pieceButton
			target.pieceVO = new PieceVO(true,target.pieceVO.name,target.pieceVO.position);
			//refresh selected pieces
			PiecesModel.getInstance().selectedPiece = null;
			//refresh gameGraph
			if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
			{
				BoardModel.getInstance().gameGraphBlue.addNode( new Node( destPositionIndex ) );
				trace("BoardModel.getInstance().gameGraphBlue.nNodes():",BoardModel.getInstance().gameGraphBlue.nNodes());
			}else
			{
				BoardModel.getInstance().gameGraphRed.addNode( new Node( destPositionIndex ) );
				trace("BoardModel.getInstance().gameGraphRed.nNodes():",BoardModel.getInstance().gameGraphRed.nNodes());
			}
			//left
			if(gbDestination.left)
			{
				var leftGB:GasketButton = BoardModel.getInstance().gasketsPosition.gett(gbDestination.left.position[0],gbDestination.left.position[1]) as GasketButton;
				trace("gbDestination's leftGB:",leftGB);
				if(leftGB!=null)
				{
					var leftGBIndex:int = PositionTransactUtil.transactArray2Index(gbDestination.left.position);
					trace("gbDestination's leftGBIndex:",leftGBIndex);
					if( PlayerModel.getInstance().gameTurnStatus == PlayerModel.getInstance().IS_BLUE_TURN_NOW )
					{
						if( leftGB.styleName == GasketButton.STYLE_TRIANGLE_BLUE 
					    	|| leftGB.styleName == GasketButton.STYLE_TRIANGLE_BLUE_R180 )
					    {
					    	BoardModel.getInstance().gameGraphBlue.addEdge( new WeightedEdge( destPositionIndex,leftGBIndex) );
							BoardModel.getInstance().gameGraphBlue.addEdge( new WeightedEdge( leftGBIndex,destPositionIndex) );
					    }
					}else
					{
						if( leftGB.styleName == GasketButton.STYLE_TRIANGLE_RED 
					    	|| leftGB.styleName == GasketButton.STYLE_TRIANGLE_RED_R180 )
					    {
							BoardModel.getInstance().gameGraphRed.addEdge( new WeightedEdge( destPositionIndex,leftGBIndex) );
							BoardModel.getInstance().gameGraphRed.addEdge( new WeightedEdge( leftGBIndex,destPositionIndex) );
					    }
					}
				}
			}	
			//top
			if(gbDestination.top)
			{
				var topGB:GasketButton = BoardModel.getInstance().gasketsPosition.gett(gbDestination.top.position[0],gbDestination.top.position[1]) as GasketButton;
				trace("gbDestination's topGB:",topGB);
				if(topGB!=null)
				{
					var topGBIndex:int = PositionTransactUtil.transactArray2Index(gbDestination.top.position);
					trace("gbDestination's topGBIndex:",topGBIndex);
					if( PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW )
					{
						if( topGB.styleName == GasketButton.STYLE_TRIANGLE_BLUE 
					    	|| topGB.styleName == GasketButton.STYLE_TRIANGLE_BLUE_R180 )
					    {
							BoardModel.getInstance().gameGraphBlue.addEdge( new WeightedEdge( destPositionIndex,topGBIndex) );
							BoardModel.getInstance().gameGraphBlue.addEdge( new WeightedEdge( topGBIndex,destPositionIndex) );
					    }
					}else
					{
						if( topGB.styleName == GasketButton.STYLE_TRIANGLE_RED 
					    	|| topGB.styleName == GasketButton.STYLE_TRIANGLE_RED_R180 )
					    {
							BoardModel.getInstance().gameGraphRed.addEdge( new WeightedEdge( destPositionIndex,topGBIndex) );
							BoardModel.getInstance().gameGraphRed.addEdge( new WeightedEdge( topGBIndex,destPositionIndex) );
					    }
					}
				}
			}	
			//right
			if(gbDestination.right)
			{
				var rightGB:GasketButton = BoardModel.getInstance().gasketsPosition.gett(gbDestination.right.position[0],gbDestination.right.position[1]) as GasketButton;
				trace("gbDestination's rightGB:",rightGB);
				if(rightGB!=null)
				{
					var rightGBIndex:int = PositionTransactUtil.transactArray2Index(gbDestination.right.position);
					trace("gbDestination's rightGBIndex:",rightGBIndex);
					if( PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW )
					{
						if( rightGB.styleName == GasketButton.STYLE_TRIANGLE_BLUE 
					    	|| rightGB.styleName == GasketButton.STYLE_TRIANGLE_BLUE_R180 )
					    {
							BoardModel.getInstance().gameGraphBlue.addEdge( new WeightedEdge( destPositionIndex,rightGBIndex ) );
							BoardModel.getInstance().gameGraphBlue.addEdge( new WeightedEdge( rightGBIndex,destPositionIndex ) );
					    }
					}else
					{
						if( rightGB.styleName == GasketButton.STYLE_TRIANGLE_RED 
					    	|| rightGB.styleName == GasketButton.STYLE_TRIANGLE_RED_R180 )
					    {
							BoardModel.getInstance().gameGraphRed.addEdge( new WeightedEdge( destPositionIndex,rightGBIndex ) );
							BoardModel.getInstance().gameGraphRed.addEdge( new WeightedEdge( rightGBIndex,destPositionIndex ) );
					    }
					}
				}
			}	
			//bottom
			if(gbDestination.bottom)
			{
				var bottomGB:GasketButton = BoardModel.getInstance().gasketsPosition.gett(gbDestination.bottom.position[0],gbDestination.bottom.position[1]) as GasketButton;
				trace("gbDestination's bottomGB:",bottomGB);
				if(bottomGB!=null)
				{
					var bottomGBIndex:int = PositionTransactUtil.transactArray2Index(gbDestination.bottom.position);
					trace("gbDestination's bottomGBIndex:",bottomGBIndex);
					if( PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW )
					{
						if( bottomGB.styleName == GasketButton.STYLE_TRIANGLE_BLUE 
					    	|| bottomGB.styleName == GasketButton.STYLE_TRIANGLE_BLUE_R180 )
					    {
							BoardModel.getInstance().gameGraphBlue.addEdge( new WeightedEdge( destPositionIndex,bottomGBIndex) );
							BoardModel.getInstance().gameGraphBlue.addEdge( new WeightedEdge( bottomGBIndex,destPositionIndex) );
					    }
					}else
					{
						if( bottomGB.styleName == GasketButton.STYLE_TRIANGLE_RED 
					    	|| bottomGB.styleName == GasketButton.STYLE_TRIANGLE_RED_R180 )
					    {
							BoardModel.getInstance().gameGraphRed.addEdge( new WeightedEdge( destPositionIndex,bottomGBIndex) );
							BoardModel.getInstance().gameGraphRed.addEdge( new WeightedEdge( bottomGBIndex,destPositionIndex) );
					    }
					}
				}
			}	
			
			//refresh adjacentCollection,oppositeCollection,hypotenuseCollection;
			if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
			{
				if(PiecesModel.getInstance().adjacentCollection.contains( gbDestination ))
				{
					PiecesModel.getInstance().adjacentCollectionBlue.addItem( gbDestination );
				}
				if(PiecesModel.getInstance().oppositeCollection.contains( gbDestination ))
				{
					PiecesModel.getInstance().oppositeCollectionBlue.addItem( gbDestination );
				}
				if(PiecesModel.getInstance().hypotenuseCollection.contains( gbDestination ))
				{
					PiecesModel.getInstance().hypotenuseCollectionBlue.addItem( gbDestination );
				}
			}else
			{
				if(PiecesModel.getInstance().adjacentCollection.contains( gbDestination ))
				{
					PiecesModel.getInstance().adjacentCollectionRed.addItem( gbDestination );
				}
				if(PiecesModel.getInstance().oppositeCollection.contains( gbDestination ))
				{
					PiecesModel.getInstance().oppositeCollectionRed.addItem( gbDestination );
				}
				if(PiecesModel.getInstance().hypotenuseCollection.contains( gbDestination ))
				{
					PiecesModel.getInstance().hypotenuseCollectionRed.addItem( gbDestination );
				}
			}
			if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
			{
				trace("BoardModel.getInstance().gameGraphBlue:",BoardModel.getInstance().gameGraphBlue.dump());
			}else
			{
				trace("BoardModel.getInstance().gameGraphRed:",BoardModel.getInstance().gameGraphRed.dump());
			}
//			trace("afterMovement(RED)@PiecesManager:",DebugUtil.dumpGameGraph(DebugUtil.TYPE_RED));
//			trace("afterMovement(BLUE)@PiecesManager:",DebugUtil.dumpGameGraph(DebugUtil.TYPE_BLUE));
			//calculateResult:
			calculateResult(BoardModel.getInstance().gamePosition);
			
			//discover protected position
			
			return true;
		}
		
		//simulation pieceButtons such as ToggleButtonBar;
		public function untoggleOtherPieces(nameFilter:String):void
		{
			var myPieceButton:PieceButton; 
			for(var i:int=0;i<PiecesModel.getInstance().bluePiecesCollection.length;i++)
			{
				myPieceButton = PiecesModel.getInstance().bluePiecesCollection.getItemAt(i) as PieceButton; 
				if(myPieceButton.pieceVO.name!=nameFilter)
				{
					myPieceButton.selected = false;
				}
			}
		}
		
		public function untoggleOtherGaskets(position:Array):void
		{
		}
		
		private static function calculateResult(gamePositon:Array2):void
		{
			if(!BoardManager.analysisGamePhases(PiecesModel.getInstance().redGasketsCollection,PiecesModel.getInstance().blueGasketsCollection))
			{
				if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
				{
					PlayerManager.isComputerTurnNow();
				}else
				{
					PlayerManager.isHumanTurnNow();	
				}
			}else
			{
				if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
				{
					PlayerManager.humanWin();
				}else
				{
					PlayerManager.computerWin();	
				}
			}
		}
	}
}